package com.example.android.service;

import java.io.IOException;
import java.util.Random;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

public class Main extends Activity {

	private class KitchenTimerReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
//			Toast toast = Toast.makeText(getApplicationContext(), "Time over!", Toast.LENGTH_LONG);
//			toast.show();
//			MediaPlayer mp = MediaPlayer.create(Main.this, R.raw.alarm);
//			try {
//				mp.start();
//				finish();
//			} catch (Exception e) {
//				// 例外は発生しない
//			}
			
			setWallpaper();
		}
	}

	private static final String TAG = "Main";

	private KitchenTimerService kitchenTimerService;
	private final KitchenTimerReceiver receiver = new KitchenTimerReceiver();

	private ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {
			kitchenTimerService = ((KitchenTimerService.KitchenTimerBinder)service).getService();
		}

		@Override
		public void onServiceDisconnected(ComponentName className) {
			kitchenTimerService = null;
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

		final TimePicker timePicker = (TimePicker)findViewById(R.id.TimePicker01);
		timePicker.setIs24HourView(true);
		timePicker.setCurrentHour(0);
		timePicker.setCurrentMinute(1);

		Button button = (Button)findViewById(R.id.Button01);
		button.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				long hour = timePicker.getCurrentHour();
				long min = timePicker.getCurrentMinute();

				kitchenTimerService.schedule((hour * 60 + min) * 60 * 1000);
				moveTaskToBack(true);
				
			}

		});

		// サービスを開始
		Intent intent = new Intent(this, KitchenTimerService.class);
		startService(intent);
		IntentFilter filter = new IntentFilter(KitchenTimerService.ACTION);
		registerReceiver(receiver, filter);

		// サービスにバインド
		bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

		// いったんアンバインドしてから再度バインド
		unbindService(serviceConnection);
		bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		unbindService(serviceConnection); // バインド解除
		unregisterReceiver(receiver); // 登録解除
		kitchenTimerService.stopSelf(); // サービスは必要ないので終了させる。
	}
	
	int currentImageId = 0;
	public void setWallpaper() {
		Log.v(TAG, "setWallpaper was called");
        Context context = this.getBaseContext();
        Bitmap bitmap = null;
        if (currentImageId == R.drawable.image) {
        	currentImageId = R.drawable.a17;
		} else {
			currentImageId = R.drawable.image;
		}
        bitmap =  BitmapFactory.decodeResource(getResources(), currentImageId);
        try {
			context.setWallpaper(bitmap);
		} catch (IOException e) {
			e.printStackTrace();
		}

}
}